using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.UI;

public class LunaStatusManager : MonoBehaviour
{
    public static LunaStatusManager Instance { get; private set; }

    public LunaState LunaState { get; set; } = LunaState.Normal;
    
    /// <summary>
    /// 血条原始长度
    /// </summary>
    private float originalWidthHPBar = 0.0f;

    /// <summary>
    /// 最大生命值
    /// </summary>
    public const float MaxHP = 100.0f;
    /// <summary>
    /// 当前生命值
    /// </summary>
    public float CurrentHP { get; private set; }

    /// <summary>
    /// 最大法力值
    /// </summary>
    public const float MaxMP = 100.0f;
    /// <summary>
    /// 当前法力值
    /// </summary>
    public float CurrentMP { get; private set; }

    /// <summary>
    /// 等级
    /// </summary>
    public int Level { get; set; } = 1;

    /// <summary>
    /// 等级和属性
    /// </summary>
    private Dictionary<int, LunaProperties> LevelProperties { get; set; } = new()
    {
        {
            1, new()
            {
                AD = 10,
                AP = 10,
                DefendProbability = 0.1f,
                Skills = new()
                {
                    new RecoverHP()
                    {
                        Name = nameof(RecoverHP),
                        CostMP = 10,
                        RecoverValue = 5
                    },
                    new Redemption()
                    {
                        Name = nameof(Redemption),
                        AP = 20,
                        CostMP = 20
                    }
                }
            }
        },
        {
            2, new()
            {
                AD = 20,
                AP = 20,
                DefendProbability = 0.2f
            }
        }
    };

    /// <summary>
    /// 血条
    /// </summary>
    public Image imageHP;
    /// <summary>
    /// 法条
    /// </summary>
    public Image imageMP;

    void Awake()
    {
        Instance = this;
        this.CurrentHP = MaxHP;
        this.CurrentMP = MaxMP;
        this.originalWidthHPBar = this.imageHP.rectTransform.rect.width;
    }

    public void SetHP(float percent)
    {
        this.SetHPMP(this.imageHP, percent);
    }

    public void SetMP(float percent)
    {
        this.SetHPMP(this.imageMP, percent);
    }

    private void SetHPMP(Image hpmp, float percent)
    {
        hpmp.rectTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, percent * this.originalWidthHPBar);

    }

    public void ChangeHP(float amount)
    {
        this.CurrentHP = Mathf.Clamp(this.CurrentHP + amount, 0, MaxHP);
        this.SetHP(GetHPPercent());
    }

    /// <summary>
    /// 改变当前法力值
    /// </summary>
    /// <param name="amount">正数：增加法力值，负数：消耗法力值</param>
    /// <returns>消耗法力前的判断，true：法力值充足，false：法力值不足</returns>
    public bool ChangeMP(float amount)
    {
        if(amount < 0 && this.CurrentMP < Mathf.Abs(amount))
        {
            // 法力值不足
            return false;
        }

        this.CurrentMP = Mathf.Clamp(this.CurrentMP + amount, 0, MaxMP);
        this.SetMP(GetMPPercent());
        return true;
    }

    private float GetHPPercent()
    {
        return this.CurrentHP / MaxHP;
    }

    private float GetMPPercent()
    {
        return this.CurrentMP / MaxMP;
    }

    public float GetAD()
    {
        var properties = this.LevelProperties[this.Level];
        return properties.AD + properties.BonusAD;
    }

    /// <summary>
    /// 判断是否可以防御
    /// </summary>
    /// <returns></returns>
    public bool CanDefend()
    {
        var value = UnityEngine.Random.value;
        var defendProbability = this.LevelProperties[this.Level].DefendProbability;
        return value <= defendProbability;
    }

    public SkillBase GetSkillInfo(string name)
    {
        var skillInfo = this.LevelProperties[this.Level].Skills.FirstOrDefault(x => x.Name == name);
        if (skillInfo != null)
        {
            return skillInfo;
        }
        return null;
    }

}